package com.lcz.web;

import com.lcz.bean.Student;
import com.lcz.service.StudentService;
import com.lcz.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class studentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取参数
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2分页数据，limit开始位置，显示条数
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值，则默认查询第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);

        //2.service
        StudentService studentService = new StudentServiceImpl();
        int usex=(sex==null||sex.length()==0?-1:Integer.parseInt(sex));
        List<Student> students = studentService.getStudents(stuname,stuno,usex,index,5);
        //获取总页数=总条数%每页显示的条数>0？总条数/每页显示条数+1：总条数/每页显示条数
        int total = studentService.total(stuname, stuno,usex);//总条数
        int totalPages=total%5>0?total/5+1:total/5;  //总页数

        //3.跳转页面
        req.setAttribute("stus",students);
        //存储模糊查询条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储分页数据
        req.setAttribute("index",index);
        req.setAttribute("size",5);
        req.setAttribute("total",total);
        req.setAttribute("totalPages",totalPages);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
